package com.yeren.jsp.tarena.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CommentFilter1 implements Filter{
	private String illegalWord;
	
	//
	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse resp=(HttpServletResponse)response;
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out=resp.getWriter();
		String comment=req.getParameter("comment");
		if(comment.indexOf("damn")!=-1){
			out.print("<h3>评论内容已关闭</h3>");
		}else{
			chain.doFilter(req,resp);
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		illegalWord=filterConfig.getInitParameter("illegalWord");
		System.out.println("Filter1:init is runnming... "+illegalWord);
	}
}
